package com.autumn.mybatis.provider.sqlserver.builder;

import com.autumn.mybatis.metadata.EntityTable;
import com.autumn.mybatis.provider.sqlserver.AbstractSqlServerProvider;
import com.autumn.mybatis.provider.sqlserver.SqlServerUtils;
import com.autumn.mybatis.provider.util.MybatisSqlUtils;

/**
 * SqlServer 兼容2012 查询生成器
 *
 * @author shao
 * @date 2017/11/21 15:55
 */
public class SqlServerQueryBuilder2012 extends AbstractSqlserverQueryBuilder {

    /**
     * @param dbProvider
     */
    public SqlServerQueryBuilder2012(AbstractSqlServerProvider dbProvider) {
        super(dbProvider);
    }


    /**
     * 分页查询
     * @param table
     *            表
     * @param parmaName
     *            参数名称
     * @return
     */
    @Override
    public String getSelectListSql(EntityTable table, String parmaName) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append(MybatisSqlUtils.getAllColumns(table));
        sql.append(" FROM ");
        sql.append(MybatisSqlUtils.getTableOrViewName(table));
        sql.append(lockMode(String.format("%s.lockMode", parmaName)));
        sql.append(SqlServerUtils.createSectionWhereSql(parmaName));
        sql.append(getOrder(table));
        sql.append(limit());
        return sql.toString();
    }


    /**
     * 查询指定列
     * @param table
     *            表
     * @param parmaName
     *            参数
     * @return
     */
    @Override
    public String getSelectBySpecifySql(EntityTable table, String parmaName) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append(getSelectBySpecifyBaseSql(parmaName));
        sql.append(" FROM ");
        sql.append(MybatisSqlUtils.getTableOrViewName(table));
        sql.append(lockMode(String.format("%s.lockMode", parmaName)));
        sql.append(SqlServerUtils.createSectionWhereSql(parmaName));
        sql.append(getOrder(table));
        sql.append(limit());
        return sql.toString();
    }

    /**
     * 根据条件查询返回一条数据
     * @param table
     *            表
     * @param parmaName
     *            参数
     * @return
     */
    @Override
    public String getSelectBySpecifyFirstSql(EntityTable table, String parmaName) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT TOP 1 ");
        sql.append("<foreach collection=\"" + parmaName + ".selectList\" item=\"item\" separator=\",\">");
        sql.append("${item}");
        sql.append("</foreach>");
        sql.append(" FROM ");
        sql.append(MybatisSqlUtils.getTableOrViewName(table));
        sql.append(lockMode(String.format("%s.lockMode", parmaName)));
        sql.append(SqlServerUtils.createSectionWhereSql(parmaName));
        sql.append(getOrder(table));
        return sql.toString();
    }


    /**
     * SqlServer 2012 数据库分页
     *
     * @return
     */
    private String limit() {
        StringBuilder sql = new StringBuilder();
        sql.append("<choose >");
        sql.append("<when test=\"section.pageClause.offset>0 and section.pageClause.limit>0\">");
        sql.append("OFFSET (#{section.pageClause.offset} * #{section.pageClause.limit})  ROW FETCH NEXT #{section.pageClause.limit} rows only  ");
        sql.append("</when>");
        sql.append("<when test=\"section.pageClause.offset>0\">");
        sql.append("OFFSET #{section.pageClause.offset} ROW");
        sql.append("</when>");
        sql.append("<when test=\"section.pageClause.limit>0\">");
        sql.append("OFFSET 0 ROW FETCH NEXT #{section.pageClause.limit} rows only");
        sql.append("</when>");
        sql.append("</choose>");
        return sql.toString();
    }

}
